﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SilverlightApplication.LanguageResources;

namespace SilverlightApplication
{
    public partial class ItemPanel : UserControl
    {
        private SearchPanels searchPanels;

        // this attribute holds 'null' when the hide/show animation is in progress
        private bool? isDeployed;
        private int itemIndex; //index of the current selected item
        private String[] itemslist;
        private static int NBR_ITEMS = 2;
        
        public ItemPanel()
        {
            InitializeComponent();
            Search.Text = LanguageLoader.GetString("Search");
            dvds.Text = LanguageLoader.GetString("dvds");
            dvds_blur.Text = LanguageLoader.GetString("dvds");
            books.Text = LanguageLoader.GetString("books");
            books_blur.Text = LanguageLoader.GetString("books");
            isDeployed = true;
            this.stbBookselEffect.Begin();
            this.itemsLoad();

            stbItemPanelShow.Completed += delegate
            {
                isDeployed = true;
            };

            stbItemPanelHide.Completed += delegate
            {
                isDeployed = false;
            };

            stbItemPanelDelayedHide.Completed += delegate
            {
                isDeployed = false;
            };

            stkPanelItems.SizeChanged += delegate
            {
                // Configure ItemPanel Initial Position
                ttItemPanel.X = 704 - Search.ActualWidth - grdBook.ActualWidth - grdDVD.ActualWidth;

                // Configure Storyboard parameters
                DAItemPanelHide.By = grdBook.ActualWidth + grdDVD.ActualWidth + 120;
                DAItemPanelDelayedHide.By = DAItemPanelHide.By;
                DAItemPanelShow.By = -DAItemPanelHide.By;
            };
        }

        private void itemsLoad()
        {
            this.itemslist = new String[NBR_ITEMS];
            this.itemslist[0] = "books";
            this.itemslist[1] = "dvds";
            itemIndex = 0; //Books is the default selected item
        }

        private void Rectangle_MouseLeave(object sender, MouseEventArgs e)
        {
           
            stbBookNonselected.Begin();
        }

        private void Search_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDeployed.HasValue)
            {
                if (isDeployed.Value)
                {
                    HidePanel();
                }
                else
                {
                    ShowPanel();
                }
            }
        }

        public void HidePanel()
        {
            if (isDeployed.HasValue)
            {
                if (isDeployed.Value)
                {
                    isDeployed = null;
                    stbItemPanelHide.Begin();
                }
            }
        }

        public void HidePanelDelayed()
        {
            if (isDeployed.HasValue)
            {
                if (isDeployed.Value)
                {
                    isDeployed = null;
                    stbItemPanelDelayedHide.Begin();
                }
            }
        }

        public void ShowPanel()
        {
            if (isDeployed.HasValue)
            {
                if (!isDeployed.Value)
                {
                    isDeployed = null;
                    stbItemPanelShow.Begin();
                }
            }
        }

        public void SetPanelsReference(SearchPanels bookreference)
        {
            searchPanels = bookreference;
        }

        private void books_MouseLeave(object sender, MouseEventArgs e)
        {
            this.stbBookNonselected.Begin();
        }

        private void books_LostFocus(object sender, RoutedEventArgs e)
        {
            this.stbBookNonselected.Begin();
        }

        private void books_MouseEnter(object sender, MouseEventArgs e)
        {
            this.stbBooksBlurry.Begin();
        }

        private void dvds_MouseEnter(object sender, MouseEventArgs e)
        {
            this.stbDVDBlurry.Begin();
        }

        private void dvds_MouseLeave(object sender, MouseEventArgs e)
        {
            this.stbDVDNonselected.Begin();
        }

        private void dvds_LostFocus(object sender, RoutedEventArgs e)
        {
            this.stbDVDNonselected.Begin();
        }

        //Book item was selected
        private void books_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.stbBookSelected.Begin();
            //if Book is not current selected -> aplies hide dvd effect
            if (this.getIndexselecteditem("books") != this.itemIndex)
            {
                this.stbDvdHide.Begin();
                searchPanels.ClearSearchResults();
            }
            HidePanelDelayed();
            this.stbBookselEffect.Begin();
            searchPanels.PanelSearchType = SearchType.Book;
            this.itemIndex = this.getIndexselecteditem("books");
        }


        //DVD item was selected
        private void dvds_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.stbDvdSelected.Begin();
            //if DVD is not current selected -> aplies hide book effect
            if (this.getIndexselecteditem("dvds") != this.itemIndex)
            {
                this.stbBookHide.Begin();
                searchPanels.ClearSearchResults();
            }
            HidePanelDelayed();
            this.stbDvdselEffect.Begin();
            searchPanels.PanelSearchType = SearchType.DVD;
            this.itemIndex = this.getIndexselecteditem("dvds");
        }

        //Return the index to p on itemslist(case it exists)
        //Case that p not exists on itemslist return -1
        private int getIndexselecteditem(string p)
        {
            int i = 0;
            while (!p.Equals(itemslist[i]) && (i < NBR_ITEMS))
            {
                i++;
            }
            if (i == NBR_ITEMS) return -1;
            return i;
        }

        private void Search_MouseEnter(object sender, MouseEventArgs e)
        {
            stbSearchOn.Begin();
        }

        private void Search_MouseLeave(object sender, MouseEventArgs e)
        {
            stbSearchOff.Begin();
        }
    }
}
